package com.pingan.generator.api.mybatis.generator.util;

import static org.mybatis.generator.internal.util.StringUtility.isTrue;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;

import com.pingan.generator.api.util.JsonUtil;

/**
 * 自动生产文件
 * @author ouyangenkun051
 *
 */
public class MyCommentGenerator implements CommentGenerator{
    private String currentDateStr;
    
    /** 配置文件. */  
    private Properties properties;  
      
    /** 父类的时间. */  
    private boolean suppressDate;  
      
    /** 父类的所有注释. */  
    private boolean suppressAllComments;  
  


    public MyCommentGenerator() {
        super();
        properties = new Properties();
        suppressDate = false;
        suppressAllComments = false;
        currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
    }

    /*   
     * Java文件加注释  
     */  
    public void addJavaFileComment(CompilationUnit compilationUnit) { 
         compilationUnit.addFileCommentLine("/**");  
         compilationUnit.addFileCommentLine("* @author "+System.getProperty("user.name"));  
         compilationUnit.addFileCommentLine("* "+compilationUnit.getType().getShortName()+".java");  
         compilationUnit.addFileCommentLine("* Copyright(C) 2018-2020 pingan公司");  
         compilationUnit.addFileCommentLine("* @date "+currentDateStr+"");  
         compilationUnit.addFileCommentLine("**/"); 
         return;
    }

    /**  
     * Mybatis的Mapper.xml文件里面的注释。  
     *  
     * @param xmlElement  
     *            the xml element  
     */ 
    public void addComment(XmlElement xmlElement) {
        return;
    }

    /*  
     * 调用此方法为根元素的第一个子节点添加注释。 此方法可用于添加  
     * 一般文件注释（如版权声明）。 但是，请注意，XML文件合并功能不会处理  
     * 这个注释。 如果反复运行生成器，则只保留初始运行的注释。  
     */ 
    public void addRootComment(XmlElement rootElement) {
        // add no document level comments by default
        return;
    }

    /*   
     * 从properties配置文件中添加此实例的属性  
     *   
     */  
    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);

        suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));

        suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
    }

    /**  
     *  此方法用于添加自定义javadoc标签。 如果您不希望包含Javadoc标签，您可能不会执行任何操作 -   
     *  但是，如果不包含Javadoc标签，那么eclipse插件的Java合并功能就会中断。  
     *  
     * @param javaElement  
     *            the java element  
     * @param markAsDoNotDelete  
     *            the mark as do not delete  
     */
    protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
    	javaElement.addJavaDocLine(" *");
        StringBuilder sb = new StringBuilder();
        sb.append(" * ");
        sb.append(MergeConstants.NEW_ELEMENT_TAG);
        if (markAsDoNotDelete) {
            sb.append(" do_not_delete_during_merge");
        }
        String s = getDateString();
        if (s != null) {
            sb.append(' ');
            sb.append(s);
        }
        javaElement.addJavaDocLine(sb.toString());
    }

    /**  
     * 获取时间  
     * @return a string representing the current timestamp, or null  
     */  
    protected String getDateString() {
        String result = null;
        if (!suppressDate) {
            result = currentDateStr;
        }
        return result;
    }
    
    /**  
     * Mapper Java类的类注释  
     */ 

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
    	 System.out.println("Mapper Java类的类注释 ");
    	 List<IntrospectedColumn> list = introspectedTable.getPrimaryKeyColumns();
    	if(list!=null && list.size()>0){
    		 innerClass.addJavaDocLine("import javax.persistence.Id;");
    		 innerClass.addJavaDocLine("import javax.persistence.Table;");
    	}
        if (suppressAllComments) {
            return;
        }
        
        innerClass.addJavaDocLine("/**");  
        innerClass.addJavaDocLine(" * @Title "+introspectedTable.getFullyQualifiedTable()+"表的实体类");  
        innerClass.addJavaDocLine(" * @Description "+introspectedTable.getFullyQualifiedTable().getDomainObjectName());  
        innerClass.addJavaDocLine(" * @version 1.0");  
        innerClass.addJavaDocLine(" * @Author ouyang");  
        innerClass.addJavaDocLine("* Copyright(C) 2018-2020 pingan公司");  
        innerClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));  
        innerClass.addJavaDocLine(" */");  
        
        StringBuilder sb = new StringBuilder();
        innerClass.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        sb.append(" ");
        sb.append(getDateString());
        innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
        innerClass.addJavaDocLine(" */");
    }

    
    /*   
     * 枚举注释  
     */  
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
        if (suppressAllComments) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        innerEnum.addJavaDocLine("/**456");
        sb.append(" * ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        innerEnum.addJavaDocLine(sb.toString().replace("\n", " "));
        innerEnum.addJavaDocLine(" */");
    }

    /*   
     * 字段注释  
     */  
    public void addFieldComment(Field field, IntrospectedTable introspectedTable,
            IntrospectedColumn introspectedColumn) {
        if (suppressAllComments) {
            return;
        }
        List<IntrospectedColumn> list = introspectedTable.getPrimaryKeyColumns();
        for (IntrospectedColumn i : list) {
        	if(introspectedColumn.getActualColumnName().equals(i.getActualColumnName())){
        		 field.addAnnotation("@javax.persistence.Id");
        		System.out.println("主键    "+JsonUtil.toJson(i.getActualColumnName()));
        	}
		}
        field.addAnnotation("@com.pingan.generator.api.annotation.ModelFiledRemark(value=\""+introspectedColumn.getRemarks()+"\")");
        StringBuilder sb = new StringBuilder();
        field.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedColumn.getRemarks());
        field.addJavaDocLine(sb.toString().replace("\n", " "));
        field.addJavaDocLine(" */");
    }

    /*   
     * 字段注释  
     */  
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        if (suppressAllComments) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        field.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        field.addJavaDocLine(sb.toString().replace("\n", " "));
        field.addJavaDocLine(" */");
    }

    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        if (suppressAllComments) {
            return;
        }
      method.addJavaDocLine("/** 789");
      addJavadocTag(method, false);
      method.addJavaDocLine(" */");
    }

    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
            IntrospectedColumn introspectedColumn) {
        if (suppressAllComments) {
            return;
        }
        method.addJavaDocLine("/**");
        StringBuilder sb = new StringBuilder();
        sb.append(" * ");
        sb.append(introspectedColumn.getRemarks());
        method.addJavaDocLine(sb.toString().replace("\n", " "));
        sb.setLength(0);
        sb.append(" * @return ");
        sb.append(introspectedColumn.getActualColumnName());
        sb.append(" ");
        sb.append(introspectedColumn.getRemarks());
        method.addJavaDocLine(sb.toString().replace("\n", " "));
        method.addJavaDocLine(" */");
    }

    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
            IntrospectedColumn introspectedColumn) {
        if (suppressAllComments) {
            return;
        }
        																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																		method.addJavaDocLine("/**");
        StringBuilder sb = new StringBuilder();
        sb.append(" * ");
        sb.append(introspectedColumn.getRemarks());
        method.addJavaDocLine(sb.toString().replace("\n", " "));
        Parameter parm = method.getParameters().get(0);
        sb.setLength(0);
        sb.append(" * @param ");
        sb.append(parm.getName());
        sb.append(" ");
        sb.append(introspectedColumn.getRemarks());
        method.addJavaDocLine(sb.toString().replace("\n", " "));
        method.addJavaDocLine(" */");
    }
   
    
    /**  
     * 类注释  
     */  
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
//    	System.out.println("Java类的类注释 ");
//    	if (suppressAllComments) {
//            return;
//        }
//        innerClass.addJavaDocLine("/** Java类的类注释");  
//        innerClass.addJavaDocLine(" * @Title "+introspectedTable.getFullyQualifiedTable()+"表的实体类");  
//        innerClass.addJavaDocLine(" * @Description "+introspectedTable.getFullyQualifiedTable().getIntrospectedTableName());  
//        innerClass.addJavaDocLine(" * @version 1.0");  
//        innerClass.addJavaDocLine(" * @Author "+System.getProperty("user.name"));  
//        innerClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));  
//        innerClass.addJavaDocLine(" */");  
    }

	@Override
	public void addModelClassComment(TopLevelClass innerClass, IntrospectedTable introspectedTable) {
		// TODO Auto-generated method stub
    	if (suppressAllComments) {
            return;
        }
//    	innerClass.addImportedType("com.ouyang.springcloud.demo.entity.base.BaseEntity");
//    	innerClass.setSuperClass("BaseEntity");//添加继承类
    	innerClass.addAnnotation("@javax.persistence.Table(name=\""+introspectedTable.getFullyQualifiedTable()+"\")");
//        innerClass.addJavaDocLine("/** 1231234444444");  
//        innerClass.addJavaDocLine(" * @Title "+introspectedTable.getFullyQualifiedTable()+"表的实体类");  
//        innerClass.addJavaDocLine(" * @Description "+introspectedTable.getFullyQualifiedTable().getIntrospectedTableName());  
//        innerClass.addJavaDocLine(" * @version 1.0");  
//        innerClass.addJavaDocLine(" * @Author "+System.getProperty("user.name")); 
//        innerClass.addJavaDocLine("* Copyright(C) 2018-2020 pingan公司");  
//        innerClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));  
//        innerClass.addJavaDocLine(" */");  
    	
    	System.out.println(innerClass.getType().getShortName());
    	//生产对应的Coll
    	AutoCreateBeanUtil.generatorBean(innerClass.getType().getShortName());
    	
	}
	
	
}
